<?php
namespace app\admin\model;

use think\facade\Db;
use think\Model;

class GoodsProp extends Model{

    public  function searchPnameAttr($query,$value)
    {
        if (!''==$value){
            $query->where('pname','like','%'.$value.'%');
        }
    }

    public function searchPropValueAttr($query,$value)
    {
        if (isset($value) && '' != $value){
            foreach ($value as $vv) {
                $query->whereFindInSet('pvalues', $vv);
            }
        }


    }
    public static function getGoodsPropList($data=[],$query=[],$page = true)
    {
        $model = self::field(true)
                ->withSearch(array_keys($data),$data);
        if($page){
            $list_rows = \think\facade\Config::has('view.list_rows')?\think\facade\Config::get('view.list_rows'):15;
            return $model->paginate([
                'list_rows' => $list_rows,
                'query'     => $query,
            ])->each(function ($v,$k){
                $shu=Db::table('ecm_goods_prop_value')
                    ->field('pid,prop_value,vid')
                    ->where("vid in ({$v['pvalues']})")
                ->select();
                $arr=[];
                foreach ($shu as $kk=>$vv){
                    $arr[]=$vv['prop_value'];
                    $a=implode(',',$arr);
                }
                $v['b']=$a;
                return $v;
            });
        }else{
            return $model->select();
        }
    }
}